package com.fangwei.utilsLibrary.customerview.roundcornerprogressbar;


import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import com.fangwei.utils.R;


/**
 * 带圆角的进度条
 * Created by fangwei on 2017/3/9.
 */

public class CornerProgressView extends View {
    /**
     * 分段颜色
     */
    private static final int[] SECTION_COLORS = {Color.GREEN, Color.YELLOW, Color.RED};
    /**
     * 进度条最大值
     */
    private float maxCount;
    /**
     * 进度条当前值
     */
    private float currentCount;
    private float lastCount;
    /**
     * 画笔
     */
    private Paint mPaint;
    private int mWidth, mHeight;
    private int backGroundColor,stokeColor,duration;

    private ValueAnimator animator;

    public CornerProgressView(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    public CornerProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CornerProgressView(Context context) {
        this(context, null);
    }

    private void initView(Context context, AttributeSet attrs,int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CornerProgressView);

        backGroundColor = typedArray.getColor(R.styleable.CornerProgressView_cornerProgressViewBackGroundColor,Color.GRAY);
        stokeColor = typedArray.getColor(R.styleable.CornerProgressView_cornerProgressViewStokeColor,Color.WHITE);
        duration = typedArray.getInt(R.styleable.CornerProgressView_cornerProgressViewDuration,1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        int round = mHeight / 2;
        System.out.println("max=" + maxCount + "  current=" + currentCount);
        mPaint.setColor(stokeColor);
        RectF rectBg = new RectF(0, 0, mWidth, mHeight);
        canvas.drawRoundRect(rectBg, round, round, mPaint);
        mPaint.setColor(backGroundColor);
        RectF rectBlackBg = new RectF(2, 2, mWidth - 2, mHeight - 2);
        canvas.drawRoundRect(rectBlackBg, round, round, mPaint);

        float section = currentCount / maxCount;
        RectF rectProgressBg = new RectF(3, 3, (mWidth - 3) * section, mHeight - 3);

        LinearGradient shader = new LinearGradient(3, 3, (mWidth - 3) * section, mHeight - 3, SECTION_COLORS, null, Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
        canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
    }

    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /***
     * 设置最大的进度值
     *
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /***
     * 设置当前的进度值
     *
     * @param currentCount
     */
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        showAnimation(lastCount, currentCount);
        lastCount = currentCount;
    }


    private void showAnimation(float last, float current) {
        animator = new ValueAnimator().ofFloat(last, current);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentCount = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }


    public float getMaxCount() {
        return maxCount;
    }

    public float getCurrentCount() {
        return currentCount;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(15);
        } else {
            mHeight = heightSpecSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

}
